package com.holyn.guofang.viewcommon.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.holyn.guofang.R;
import com.holyn.guofang.utils.ValidUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Holyn on 2017/6/8.
 */

public class ValidMethodDialog extends Dialog {
    private Context context;
    public static final int STATUS_PASSWORD = 0;
    public static final int STATUS_VALID_CODE = 1;

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;

    private OnSureClickListener onSureClickListener = null;
    private OnCancelClickListener onCancleClickListener = null;

    public interface OnSureClickListener{
        void onSureClick(String value);
    }

    public interface OnCancelClickListener{
        void onCancelClick();
    }

    public ValidMethodDialog(@NonNull Context context) {
        super(context, com.holyn.mylibrary.R.style.custom_dialog);
        this.context = context;
        initView();
    }

    public ValidMethodDialog(@NonNull Context context, OnSureClickListener onSureClickListener, OnCancelClickListener onCancleClickListener) {
        super(context, com.holyn.mylibrary.R.style.custom_dialog);
        this.context = context;
        initView();
        this.onSureClickListener = onSureClickListener;
        this.onCancleClickListener = onCancleClickListener;

//        setOnDismissListener(new OnDismissListener() {
//            @Override
//            public void onDismiss(DialogInterface dialog) {
//                if (onCancleClickListener != null){
//                    onCancleClickListener.onCancelClick();
//                }
//            }
//        });
    }

    private void initView() {
        View dialogView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_valid_method, null);
        setContentView(dialogView);
        ButterKnife.bind(this);
        setCanceledOnTouchOutside(false);
    }

    public OnSureClickListener getOnSureClickListener() {
        return onSureClickListener;
    }

    public void setOnSureClickListener(OnSureClickListener onSureClickListener) {
        this.onSureClickListener = onSureClickListener;
    }

    @OnClick({R.id.tv_sure, R.id.tv_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_sure:
                if (onSureClickListener != null) {
                    String value = etPassword.getText().toString().trim();
                    if (TextUtils.isEmpty(value)){
                        Toast.makeText(context, "请输入密码", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (!ValidUtil.isContainWord(value) || !ValidUtil.isGreaterMinLength(6, value)) {
                        Toast.makeText(context, "密码要求最少6位的字母与数字组合", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    onSureClickListener.onSureClick(value);
                    ValidMethodDialog.this.dismiss();
                } else {
                    ValidMethodDialog.this.dismiss();
                }
                break;
            case R.id.tv_cancel:
                if (onCancleClickListener != null){
                    onCancleClickListener.onCancelClick();
                }
                ValidMethodDialog.this.dismiss();
                break;
        }
    }
}
